import java.util.*;

public class Test {
}
class Solution {
    public List<String> buildArray(int[] target, int n) {
        List<String> list = new LinkedList<>();
        int j = 0;
        for(int i=1; i<=n && j<target.length; i++){
            list.add("Push");
            if(i != target[j]){
                list.add("Pop");
            }else{
                j++;
            }
        }
        return list;
    }
}
class Solution1 {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int[] count = new int[nums1.length];
        for(int i=0; i<nums1.length; i++){
            for(int j=0; j<nums2.length; j++){
                if(nums1[i] == nums2[j]){
                    j++;
                    while(j < nums2.length){
                        if(nums1[i] < nums2[j]){
                            count[i] = nums2[j];
                            break;
                        }
                        j++;
                    }
                    if(j == nums2.length){
                        count[i] = -1;
                    }
                }
            }
        }
        return count;
    }
}
class Solution2 {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Map<Integer,Integer> map = new HashMap<>();
        Stack<Integer> stack = new Stack<>();
        for(int i=nums2.length-1; i>=0; i--){
            int num = nums2[i];
            while(!stack.isEmpty() && stack.peek()<=num){
                stack.pop();
            }
            map.put(num,stack.isEmpty() ? -1 : stack.peek());
            stack.push(num);
        }
        int[] ans = new int[nums1.length];
        for(int i=0; i<nums1.length; i++){
            ans[i] = map.get(nums1[i]);
        }
        return ans;
    }
}